package com.ys.controller.web.hr;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.hr.ThrUserEntryApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.config.MenuId;
import com.ys.entity.hr.ThrUserEntryApply;
import com.ys.entity.hr.ThrUserIdCardPhoto;
import com.ys.entity.sys.YsProcInst;
import com.ys.enume.flow.YsProcInstEnum;
import com.ys.service.sys.CommonApiService;
import com.ys.util.DateUtil;

/**
 * 员工入职申请
 */
@Controller
@RequestMapping("/web/hr/thrUserEntryApply")
public class WebThrUserEntryApplyController {
	private static final Logger logger = LoggerFactory.getLogger(WebThrUserEntryApplyController.class);


	@Autowired
	private CommonApiService<ThrUserEntryApply> commonApiService;

	@Autowired
	private ThrUserEntryApplyService thrUserEntryApplyService;
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){
		ThrUserEntryApply entity = new ThrUserEntryApply();
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.THR_UserEntryApply);
			entity.setBid(bid);
			Map<String, Object> extraParam = new HashMap<String, Object>();
			extraParam.put("userId", "保存后自动生成");
			extraParam.put("roleIdAttribute", "职能");
			Date nowDate = new Date();
			extraParam.put("entryDate", DateUtil.getYYYY_MM_DD(nowDate));//入职时间
			// 获取当前日期
			LocalDate currentDate = LocalDate.now();
			// 添加三个月
			LocalDate threeMonthsLater = currentDate.plusMonths(3);
			// 获取三个月后的年、月、日
			int year = threeMonthsLater.getYear();
			int month = threeMonthsLater.getMonthValue();
			int day = threeMonthsLater.getDayOfMonth();
			extraParam.put("formalDate", year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day));//转正时间、默认3个月后
			return commonApiService.toAdd(model, entity, MenuId.THR_UserEntryApply, extraParam, null);
		} catch (Exception e) {
			logger.error("员工入职申请新增页面出错：",e.getMessage(),e);
			return com.bo.vo.W.error(model, e.getMessage());
		}
	}







	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,String bid){
		try {
			Map<String,Object> entity = thrUserEntryApplyService.getExtraParam(bid);
			//获取审核时间
			YsProcInst ysProcInst = new YsProcInst().setBid(entity.get("bid").toString()).queryFirst();

			Integer onProcess = ysProcInst.getStateFlow() == YsProcInstEnum.StateFlow.INIT.value ? 0 : 1;
			model.addAttribute("onProcess", onProcess);
			return commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new ThrUserEntryApply(), MenuId.THR_UserEntryApply, entity, null);
		} catch (Exception e) {
			logger.error("员工入职申请编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}



	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,String bid){
		try {
			Map<String,Object> entity = thrUserEntryApplyService.getExtraParam(bid);


			model.addAttribute("onProcess", 1);
			//return commonApiService.toEdit(model, Long.parseLong(entity.get("id").toString()), new ThrUserEntryApply(), MenuId.THR_UserEntryApply_Detail, extraParam);
			return commonApiService.toDetail(model, Long.parseLong(entity.get("id").toString()), new ThrUserEntryApply(), MenuId.THR_UserEntryApply, entity, null);

		} catch (Exception e) {
			logger.error("员工入职申请编辑页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


}






